"""
列表推导式
"""
# a = [i for i in range(1, 10) if i % 2 == 0]
# print(a)
# a = [i for i in range(1, 101) if i % 3 == 0]
# print(a)

# b = ["123", "2", "21312", "1212", "12312"]
# c = [i for i in b if len(i) > 3]
# print(c)

# title 是一段话中的每个单词的首字母都大写
# capitalize 是只有首字母大写
# b = ["roc", "jack", "tom", "alis", "block"]
# c = [i.capitalize() for i in b if len(i) > 1]
# print(c)

# 列表推导式 多层循环 得到 [('偶数','奇数'),('偶数','奇数'),('偶数','奇数').....]
# 取 0-5中的偶数和0-10中的奇数组成的元组集合
# 例如 : [(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9)]
# a = [(i, j) for i in range(0, 5) if i % 2 == 0 for j in range(0, 10) if j % 2 != 0]
# print(a)

# 从 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 得到 [3,6,9]
# a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# b = [i[2] for i in a]
# print(b)

# map1 = {"name": "a", "age": 1, "salary": 100}
# map2 = {"name": "b", "age": 2, "salary": 200}
# map3 = {"name": "c", "age": 3, "salary": 300}
#
# list = [map1, map2, map3]
#
# 年龄大于2的加100  年龄小于等于2的加20
# r = [i["salary"] + 100 if i["age"] > 2 else i["salary"] + 20 for i in list]
# print(r)
from typing import Iterable

"""
集合推导式
"""
# list = [1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5]
# set = {i + 1 for i in list}
# print(set)

"""
字典推导式
"""
# map1 = {"name": "a", "age": 1, "salary": 100}
# map2 = {value: key for key, value in map1.items()}
# print(map2)

"""
生成器generator 推导式换成小括号
"""

# g = (i + 1 for i in range(2))

# print(type(g))
# print(g.__next__())
# print(next(g))

# while True:
#     try:
#         print(next(g))
#     except StopIteration as e:
#         print("没有更多元素了")
#         break

# 创建生成器
# def func():
#     n = 0
#     while True:
#         n += 1
#         yield n
#
#
# g = func()
# print(g.__next__())

# 菲比特
# def fib(length):
#     a, b = 0, 1
#     n = 0
#     while n < length:
#         yield b
#         a, b = b, a + b
#         n += 1
#     return "没有更多元素"
#
#
# g = fib(8)
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())

# def func():
#     n = 0
#     while True:
#         temp = yield n
#         print("temp", temp)
#         n += 1
#
#
# g = func()
# # print(g.__next__())
# # print(g.__next__())
# # print(g.__next__())
#
# g.send(None)
# print(g.send("哈哈"))
# print(g.send("呵呵"))


# 进程 > 线程 > 协程
# 迅雷 : 先将电影分割成多个块 然后分线程下载
# 交替执行

# def task1(n):
#     for i in range(n):
#         print("正在搬第{}块砖".format(i))
#         yield None
#
#
# def task2(n):
#     for i in range(n):
#         print("正在听第{}首歌".format(i))
#         yield None
#
#
# g1 = task1(10)
# g2 = task2(10)
#
# while True:
#     try:
#         g1.__next__()
#         g2.__next__()
#     except StopIteration as e:
#         break

# 判断一个元素是否可以迭代
# l = [1, 2, 3]
# print(isinstance(l, Iterable))


# 迭代器
li = [i for i in range(5)]
lii = iter(li)
print(lii.__next__())
print(next(lii))
